<?php

namespace app\crm\model;

use app\admin\model\ShopModel;
use think\facade\Db;
use app\common\traits\ActionRecordTrait;
use think\Model;
use app\admin\controller\ApiCommon;
use app\mini\model\PersonCase;

class AddCaseModel extends Model
{
	use ActionRecordTrait;

    /**
     * 表名
     * @var string
     */
    protected $name = 'case';
    protected $deleteTime = 'deleted_at';
    protected $defaultSoftDelete = 0;

    public function customer(){
        return $this->hasOne(Customer::class,'id','customer_id');
    }
    public function shop(){
        return $this->hasOne(ShopModel::class,'shop_id','shop_id');
    }


    /**
     * 客户详情-新增优案
     * @param  array $request 请求参数
     * @return array
     * @author hz
     * @create 2021-10-26
     */
    public function AddCustomerCase($request)
    {
        $album = Db::name('customer_album')->where('id',$request['addAnliId'])->find();


        $shop_id = Db::name('customer')->where('id',$album['customer_id'])->value('shop_id');



        // 品项一级分类
        $item_one = $album['item_one'];

        // 品项二级分类
        $item_two = $album['item_two'];

        // 品项三级分类
        $item_three = $album['item_three'];

        $graduation_type = $request['is_excellent']+1;

        if($request['graduation_type'] == 0){
            // 申请优案
            $sq_type = 1;
            $is_excellent = 1;
        }

        if($request['graduation_type'] == 1){
            // 申请满意
            $sq_type = 2;
            $is_excellent = 0;
        }


        $where = [];

        array_push($where,['customer_id','=',$album['customer_id']]);
        array_push($where,['item_one','=',$item_one]);
        array_push($where,['item_two','=',$item_two]);
        array_push($where,['item_three','=',$item_three]);


        $albumList = Db::name('customer_album')->where($where)->select()->toArray();

        // echo "<pre>";
        // print_r($albumList);
        // echo "</pre>";
        // exit;

        $setting = array($item_one,$item_two,$item_three);
        $sD = Db::name('item_type')->where('id','in',$setting)->column('name','id');
        $nurse_id = Db::name('customer')->where('id','=',$album['customer_id'])->value('nurse_id') ?? 0;

        $req = [
            'customer_id'     => $album['customer_id'],
            'shop_id'         => $shop_id,
            'item_one'        => $album['item_one'],
            'item_two'        => $album['item_two'],
            'item_three'      => $album['item_three'],
            'graduation_type' => $graduation_type,
            'sq_type'         => $sq_type,
            'is_excellent'    => $is_excellent,
        ];

        // echo "<pre>";
        // print_r($req);
        // echo "</pre>";
        // exit;
        $addModel = new PersonCase();

        $addRes = $addModel->addCase($req);

        // echo "<pre>";
        // print_r($addRes);
        // echo "</pre>";
        // exit;
        if($addRes['error'] != '创建成功'){

            return ['error' => $addRes['error'], 'data' => []];

        }

        // $addRes['data']['id'] = 23499;
        if($addRes['data']['id']){

            // 申报评级
            $setTjReq = [
                'case_id' => $addRes['data']['id'],
                'type'    => $sq_type,
            ];

            $setRes = $addModel->setTj($setTjReq);

            // 增加治疗记录
            $image = [];

            foreach ($albumList as $key => $value) {
                array_push($image,$value['src']);
            }

            $addCaseLogReq = [
                'case_id' => $addRes['data']['id'],
                'content' => $request['return_visit_record'] ?? '',
                'iamges'  => $image,
                'ht'      => 1,
            ];

            $addLogRes = $addModel->addCaseLog($addCaseLogReq);
            // echo "<pre>";
            // print_r($addLogRes);
            // echo "</pre>";
            // exit;
            if($addLogRes['data']['id']){
                return ['error' => '创建成功', 'data' => []];
            }

        }
    }

}
